package XynLeen.GraduationProject.Fragments.Personal;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import java.util.List;

import XynLeen.GraduationProject.Activities.Activity_Album;
import XynLeen.GraduationProject.Activities.Activity_Personal_Message;
import XynLeen.GraduationProject.Bean.BudgetBean;
import XynLeen.GraduationProject.Bean.ScheduleBean;
import XynLeen.GraduationProject.Bean.UserBean;
import XynLeen.GraduationProject.R;

public class FragmentC extends Fragment implements View.OnClickListener{

    private TextView mBudget;
    private TextView mSchdule;
    private Button mEtMessage;
    private Button mEtProfile;

    BudgetBean mBud;
    UserBean mUser;
    ScheduleBean mSch;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_c,container,false);

        mEtMessage=view.findViewById(R.id.edit_message);
        mEtMessage.setOnClickListener(this);
        mEtProfile=view.findViewById(R.id.edit_profile);
        mEtProfile.setOnClickListener(this);
        mBudget=view.findViewById(R.id.bud_left);
        mSchdule=view.findViewById(R.id.sch_left);


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();

    }

    public void initData() {
        //查询预算
        UserBean bean=DataSupport.find(UserBean.class,1);
        double allnum=bean.getBudget();

        int number=0;
        Cursor c=DataSupport.findBySQL("select * from budgetbean");
        number=c.getCount();
        if(number!=0){
            double outnum=Double.parseDouble(DataSupport.where("cost<0").sum(BudgetBean.class,"cost",String.class));
            double innum=Double.parseDouble(DataSupport.where("cost>0").sum(BudgetBean.class,"cost",String.class));
            double leftnum=allnum+outnum+innum;
            mBudget.setText(String.valueOf(leftnum));
        }else {
            mBudget.setText(String.valueOf(allnum));
        }

        //查询事项
        int numberr=0;
        Cursor s=DataSupport.findBySQL("select * from schedulebean");
        numberr=s.getCount();
        if(numberr!=0){
            int schdule=DataSupport.count(ScheduleBean.class);
            mSchdule.setText(String.valueOf(schdule));
        }else {
            mSchdule.setText(String.valueOf(0));
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.edit_message:
                startActivity(new Intent(getActivity(), Activity_Personal_Message.class));
                break;
            case R.id.edit_profile:
                break;
        }
    }
}
